import axios from "axios"
import { ElMessage } from "element-plus"

const request = axios.create({
    baseURL: 'http://localhost:8088',
    timeout: 30000
})

//request拦截器
//可以在请求发送前对请求做统一的处理
request.interceptors.request.use(config=>{
    config.headers['Content-Type'] = 'application/json;charset=utf-8'
    return config
},error =>{
    return Promise.reject(error)
})

//response 拦截器
//可以在接口响应后统一处理结果
request.interceptors.response.use(
    response => {
        let res = response.data
        if(typeof res === 'string'){
            res = res? JSON.parse(res):res
        }
        return res
    },
    error =>{
        if(error.response.status === 404){
            ElMessage.error('未找到请求接口')
        }else if (error.response.status === 500){
            ElMessage.error('系统异常，请查看后端控制台报错')
        }else{
            console.error(error.message)
        }
        return Promise.reject(error)
    }
)

export default request